﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeneralClasses;

namespace Interface
{
    public partial class frmTitles : Form
    {
        Form opener;

        Title selectedTitle;
        List<Title> titles;

        public frmTitles(Form Opener)
        {
            opener = Opener;
            InitializeComponent();
        }

        public void refresh()
        {
            selectedTitle = null;

            titles = Title.getAllTitles();
            dgvTitles.Rows.Clear();

            txtAddName.Text = "";
            txtEditName.Text = "";

            clbAddSubTitles.Items.Clear();
            clbEditSubTitles.Items.Clear();

            foreach (Title item in titles)
            {
                clbAddSubTitles.Items.Add(item.Name);
                clbEditSubTitles.Items.Add(item.Name);
            }

            foreach (Title item in titles)
            {
                String subTitles = "";
                /*
                foreach (Title item in item.subtitles)
	            {
		            subTitles += item.Name + " ";
	            }
                */
                dgvTitles.Rows.Add(item.Name, subTitles);
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmTitles_FormClosing(object sender, FormClosingEventArgs e)
        {
            opener.Show();
        }

        private void dgvTitles_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvTitles.SelectedRows.Count > 0)
            {
                String titleName = dgvTitles.SelectedRows[0].Cells[0].Value.ToString();

                foreach (Title item in titles)
                {
                    if (item.Name == titleName)
                    {
                        selectedTitle = item;
                        break;
                    }
                }

                if (selectedTitle != null)
                {
                    txtEditName.Text = selectedTitle.Name;
                    //add subtitles to clbEditSubTitles
                }
            }
        }

        private void frmTitles_Shown(object sender, EventArgs e)
        {
            refresh();
        }

        private void btnAddTitle_Click(object sender, EventArgs e)
        {
            selectedTitle = new Title();
            selectedTitle.Name = txtAddName.Text;
            //put sub titles into selectedTitle

            if (selectedTitle.saveData() > 0)
            {
                refresh();
            }
            else
            {
                MessageBox.Show("Problem adding title!");
            }
        }

        private void btnDeleteTitle_Click(object sender, EventArgs e)
        {
            if (selectedTitle != null)
            {
                if (selectedTitle.removeData() == 0)
                {
                    refresh();
                }
                else
                {
                    MessageBox.Show("Problem deleting title!");
                }
            }
        }

        private void btnEditSubTitles_Click(object sender, EventArgs e)
        {
            if (selectedTitle != null)
            {
                selectedTitle.Name = txtEditName.Text;
                //put edited sub titles into selectedTitle

                if (selectedTitle.saveData() > 0)
                {
                    refresh();
                }
                else
                {
                    MessageBox.Show("Problem editing title!");
                }
            }
        }

        private void txtAddName_TextChanged(object sender, EventArgs e)
        {
            Input_Constraint.keepOnly(txtAddName, true, true, true, false, null);
        }

        private void txtEditName_TextChanged(object sender, EventArgs e)
        {
            Input_Constraint.keepOnly(txtEditName, true, true, true, false, null);
        }
    }
}
